package com.pureut.scheduling.controller;

import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.scheduling.domain.BackWeight;
import com.pureut.scheduling.domain.SchedulingPlan;
import com.pureut.scheduling.domain.SchedulingPlan2;
import com.pureut.scheduling.domain.dto.SchedulingPlanDto;
import com.pureut.scheduling.domain.vo.SchedulingPlanVo;
import com.pureut.scheduling.domain.vo.UpdateSaleVo;
import com.pureut.scheduling.service.ISchedulingPlan2Service;
import com.pureut.scheduling.service.ISchedulingPlanService;

import com.pureut.scheduling.service.SchedulingPlanService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description 排程计划
 * @Date 22-12-16
 * @Author hl
 **/
@RestController
@RequestMapping("/productionPlan")
public class SchedulingPlanController extends BaseController {

    @Resource
    private ISchedulingPlanService schedulingPlanService;

    @Resource
    private ISchedulingPlan2Service iSchedulingPlan2Service;

    /**
     * 临时
     */
    @Resource
    private ISchedulingPlan2Service sps;

    /**
     * 查询排程计划信息
     */
    @RequiresPermissions("schedule:plan:productionPlan:list")
    @GetMapping("/list")
    public TableDataInfo list(SchedulingPlanVo schedulingPlanVo) {
        startPage();
        List<SchedulingPlanDto> list = schedulingPlanService.selectPlanList(schedulingPlanVo);
        return getDataTable(list);
    }

    /**
     * 确认排程计划信息
     */
    @RequiresPermissions("schedule:plan:productionPlan:confirm")
    @Log(title = "排程计划", businessType = BusinessType.INSERT)
    @PutMapping("/confirm")
    public AjaxResult confirm(@RequestBody SchedulingPlanVo schedulingPlanVo) throws Exception {
        return toAjax(schedulingPlanService.insertSchedulingPlan(schedulingPlanVo));
    }

    /**
     * 排程计划退回
     */
    @RequiresPermissions("schedule:plan:productionPlan:return")
    @Log(title = "计划退回", businessType = BusinessType.UPDATE)
    @PutMapping("/back")
    public AjaxResult editType(@RequestBody SchedulingPlan schedulingPlan) {
        return toAjax(schedulingPlanService.updatePlanBackEdit(schedulingPlan));
    }

    /**
     * 排程计划终止
     */
    @RequiresPermissions("schedule:plan:productionPlan:stop")
    @Log(title = "计划终止", businessType = BusinessType.UPDATE)
    @PutMapping("/theEnd")
    public AjaxResult theEnd(@RequestBody SchedulingPlan schedulingPlan) {
        return toAjax(schedulingPlanService.updatePlanTheEndEdit(schedulingPlan));
    }

    /**
     * 根据编号获取详细信息
     */
    @RequiresPermissions("schedule:plan:productionPlan:query")
    @GetMapping(value = "/{planId}")
    public AjaxResult getSchedulingPlanInfo(@PathVariable long planId) {
        return AjaxResult.success(schedulingPlanService.selectSchedulingPlanById(planId));
    }

    /**
     * 新增计划
     */
    @Log(title = "排产计划", businessType = BusinessType.INSERT)
    @PostMapping("/addPlan")
    public AjaxResult addPlan(@RequestBody List<SchedulingPlanVo> planList) {
        return toAjax(schedulingPlanService.insertSchedulingOrderPlan(planList));
    }

    @PutMapping("/planBack")
    public AjaxResult planBack(@RequestBody BackWeight backWeight) {
        return AjaxResult.success(schedulingPlanService.updatePlanBack(backWeight));
    }

    /**
     * 修改排程委外重量（扣减）
     *
     * @param backWeight
     * @return
     */
    @PutMapping("/planBackDelete")
    public AjaxResult planBackDelete(@RequestBody BackWeight backWeight) {
        return AjaxResult.success(schedulingPlanService.updatePlanBackDelete(backWeight));
    }

    /**
     * 分切完成关结且水刺关结完成修改排程单和计划单状态为已完成
     */
    @PutMapping("/updatePlanFinishStatus")
    public AjaxResult updatePlanFinishStatus(@RequestBody BackWeight backWeight) {
        return AjaxResult.success(schedulingPlanService.updatePlanFinishStatus(backWeight));
    }


    /**
     * 通过计划单号获取对应销售订单号(fegin调用)
     *
     * @return
     */
    @GetMapping("/getPlanData")
    public String getPlanData(String planCode) {
        return schedulingPlanService.getPlanData(planCode);
    }

    /**
     * 查询排程计划信息(状态为“待排产、生产中”的单据)
     */
    @GetMapping("/getListByStatus")
    public TableDataInfo getListByStatus() {
        startPage();
        List<SchedulingPlanDto> list = schedulingPlanService.getListByStatus();
        return getDataTable(list);
    }

    /**
     * 通过计划单号查出销售订单号和怕物料编码
     */
    @PostMapping("/getPlanInfo")
    public SchedulingPlanDto getPlanInfo(String planCode) {
        return schedulingPlanService.getPlanInfo(planCode);
    }

    /**
     * 修改排程单状态
     */
    @PutMapping("/updatePlanStatus")
    public boolean updatePlanStatusUnit(@RequestBody UpdateSaleVo updateSaleVo) {
        return schedulingPlanService.updatePlanStatusUnit(updateSaleVo);
    }


    /**
     * 同步ERP
     *
     * @return
     */
    @Transactional
    @PostMapping("/getDataFromERP")
    public AjaxResult getDataFromERP() {
        List<SchedulingPlan2> schedulingPlan2List = iSchedulingPlan2Service.list();
        List<SchedulingPlan> schedulingPlanList = schedulingPlan2List.stream()
                .map(schedulingPlan2 -> {
                    SchedulingPlan schedulingPlan = new SchedulingPlan();
                    try {
                        BeanUtils.copyProperties(schedulingPlan2, schedulingPlan);
                        schedulingPlan.setPlanId(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return schedulingPlan;
                }).collect(Collectors.toList());
        return toAjax(schedulingPlanService.saveBatch(schedulingPlanList));
    }


}
